<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>MySQL5.7 二进制源码包安装 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>MySQL5.7 二进制源码包安装</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/MySQL/">MySQL</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;一般平时安装MySQL都是源码包安装的，但是由于它的编译需要很长的时间，所以建议安装二进制免编译包。可以到<a href="http://dev.mysql.com/downloads" target="_blank" rel="external">MySQL官方网站</a>去下载，也可以到<a href="http://syslab.comsenz.com/downloads/linux/" target="_blank" rel="external">comsenz官方网站</a>下载，还有各大镜像站下载。</p>
<h2 id="下载安装包，并安装依赖包"><a href="#下载安装包，并安装依赖包" class="headerlink" title="下载安装包，并安装依赖包"></a>下载安装包，并安装依赖包</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">wget http://mirrors.sohu.com/mysql/MySQL-5.7/mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz</div><div class="line">yum -y install gcc-c++ ncurses-devel cmake make perl gcc autoconf automake zlib libxml libgcrypt libtool bison</div></pre></td></tr></table></figure>
<h2 id="解压"><a href="#解压" class="headerlink" title="解压"></a>解压</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">tar zxvf mysql-5.7.12-linux-glibc2.5-x86_64.tar.gz</div><div class="line">mv mysql-5.7.12-linux-glibc2.5-x86_64 /usr/<span class="built_in">local</span>/mysql</div></pre></td></tr></table></figure>
<h2 id="初始化"><a href="#初始化" class="headerlink" title="初始化"></a>初始化</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div></pre></td><td class="code"><pre><div class="line">useradd -M -s /sbin/nologin mysql </div><div class="line">mkdir -p /data/mysql</div><div class="line">chown mysql /data/mysql</div><div class="line"><span class="built_in">cd</span> /usr/<span class="built_in">local</span>/mysql</div><div class="line">./bin/mysqld --initialize --user=mysql --datadir=/data/mysql</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;注意，这一步最后一行会有一个提示</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">[Note] A temporary password is generated <span class="keyword">for</span> root@localhost: B*s1i(*,kXwg</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;最后面的字符串为root密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">./bin/mysql_ssl_rsa_setup --datadir=/data/mysql</div></pre></td></tr></table></figure>
<h2 id="拷贝配置文件和启动脚本"><a href="#拷贝配置文件和启动脚本" class="headerlink" title="拷贝配置文件和启动脚本"></a>拷贝配置文件和启动脚本</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">cp support-files/my-default.cnf /etc/my.cnf </div><div class="line">vim /etc/my.cnf //编辑或者修改</div><div class="line">basedir = /usr/<span class="built_in">local</span>/mysql</div><div class="line">datadir = /data/mysql</div><div class="line">port = 3306</div><div class="line">socket = /tmp/mysql.sock</div><div class="line"></div><div class="line">cp support-files/mysql.server /etc/init.d/mysqld</div><div class="line">vi /etc/init.d/mysqld //编辑或者修改</div><div class="line">basedir=/usr/<span class="built_in">local</span>/mysql</div><div class="line">datadir=/data/mysql</div></pre></td></tr></table></figure>
<h2 id="启动服务"><a href="#启动服务" class="headerlink" title="启动服务"></a>启动服务</h2><figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/mysqld start</div></pre></td></tr></table></figure>
<h2 id="设置root密码"><a href="#设置root密码" class="headerlink" title="设置root密码"></a>设置root密码</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用初始化密码登录</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/mysql/bin/mysql -uroot -p<span class="string">'B*s1i(*,kXwg'</span> //进入后直接设置密码</div><div class="line">mysql&gt;<span class="built_in">set</span> password = password(<span class="string">'mypass'</span>); //一定要设置一下新密码</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;退出来，再使用新的密码登录就可以了</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;还有一种情况，就是不知道初始化密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">vim /etc/my.cnf</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 [mysql] 下面增加一行</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">skip-grant-tables</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;重启mysql</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/mysqld restart</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;这时登录mysql不需要密码，进入mysql重新设置root密码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div></pre></td><td class="code"><pre><div class="line">/usr/<span class="built_in">local</span>/mysql/bin/mysql -uroot </div><div class="line">mysql&gt; update user <span class="built_in">set</span> authentication_string=password(<span class="string">'123333'</span>) <span class="built_in">where</span> user=<span class="string">'root'</span>;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;退出来后，更改my.cnf，去掉刚加的skip-grant-tables</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;再次重启mysql</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">/etc/init.d/mysqld restart</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;此时就可以使用新的密码登录了。</p>

	

	
		<span class="different-posts"><a href="/2017/10/12/MySQL/4. MySQL5.7 二进制源码包安装/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
